/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.controller.model.dto;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.AbstractDto;
import net.siufung.core.utils.CopyUtil;
import net.siufung.core.utils.ValidationUtil;
import net.siufung.core.utils.ValidationUtil.ErrorMessage;
import net.siufung.core.utils.ValidationUtil.ValidResult;
import net.siufung.market.base.permit.enums.AccessStates;
import net.siufung.market.base.permit.mapper.entity.Menu;
import net.siufung.market.base.permit.mapper.entity.Permit;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统菜单表 修改数据传参对象
 * </p>
 * @author 陈建峰
 * @since 2022-07-05 22:19:50
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value="MenuUpdateDto", description="系统菜单表修改数据传输对象")
public class MenuUpdateDto extends AbstractDto {

    private static final long serialVersionUID = -5321486892023433668L;

    /**
     * 主键id
     */
    @ApiModelProperty(value = "主键id")
    @NotNull(message = "主键id不能为空!")
    private Integer id;

    /**
     * 菜单名称
     */
    @ApiModelProperty(value = "菜单名称")
    @NotBlank(message = "菜单名称不能为空!")
    @Length(max=32, message="菜单名称长度不大于32!")
    private String menuName;

    /**
     * 访问类型
     */
    @ApiModelProperty(value = "访问类型 ")
    @NotNull(message = "访问类型不能为空!")
    private Boolean accessState;

    /**
     * 菜单路由
     */
    @ApiModelProperty(value = "菜单路由")
    @Length(max=255, message="菜单路由长度不大于255!")
    private String router;

    /**
     * 菜单图标
     */
    @ApiModelProperty(value = "菜单图标")
    @Length(max=255, message="菜单图标长度不大于255!")
    private String icon;

    /**
     * 菜单排序
     */
    @ApiModelProperty(value = "菜单排序")
    private Integer sequence;

    /**
     * 菜单描述
     */
    @ApiModelProperty(value = "菜单描述")
    @Length(max=255, message="菜单描述长度不大于255!")
    private String description;

    /**
     * 权限列表
     */
    @ApiModelProperty(value = "权限列表")
    private List<PermitText> permitTextList = new ArrayList<>();

    @Override
    public ValidResult validResult() {
        ValidResult validResult = ValidationUtil.validateBean(this);
        if(AccessStates.IS.equals(AccessStates.get(accessState))){
            if(permitTextList.size() > 0){
                for (PermitText permitText: permitTextList) {
                    permitText.validated();
                }
                if(permitTextList.size() !=
                        permitTextList.stream().map(PermitText::getSign).distinct().count()){
                    validResult.addError(new ErrorMessage("permitTextList", "权限列表存在重复数据！"));
                }
            }
        }
        return validResult;
    }

    /**
     * Menu 转化成 MenuSaveDto 数据领域实体
     * @return Menu
     */
    public Menu to(){
        Menu menu = new Menu();
        CopyUtil.copyProperties(this, menu);
        return menu;
    }

    public List<Permit> convertPermitList(){
        List<Permit> permitList = Lists.newArrayList();
        Permit permit;
        if(ObjectUtil.isNotEmpty(permitTextList)){
            for (PermitText permitText: permitTextList) {
                permit = new Permit();
                CopyUtil.copyProperties(permitText, permit);
                permitList.add(permit);
            }
        }
        return permitList;
    }
}